import {Menu} from "@/interface/menu";

//菜单数组=>菜单树
export const LoadMenuTree = (parents: Menu[], children: Menu[]) => {
  parents.sort((x,y)=>x.sort_id-y.sort_id)//根据数组中对象元素的sort_id进行排序
  parents.forEach((p) => {
    children.forEach((c, i) => {
      if (c.parent_id === p.id) {
        let _c = JSON.parse(JSON.stringify(children))
        _c.splice(i, 1) //删除当前子菜单
        LoadMenuTree([c], _c)
        if (!!p.children) {
          p.children.push(c)
          p.children.sort((x, y) => x.sort_id - y.sort_id) //根据数组中对象元素的sort_id进行排序
        } else {
          p.children = [c]
        }
      }
    })
  })
}
